package cn.xiaolang.function.file_manage.io;

import cn.xiaolang.common.utils.Monitor;
import com.google.common.io.Files;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

@Slf4j
public class FileHelperSample {

    public static void main(String[] args) throws Exception {
        testFileCopy();
    }

    /**
     * 测试文件复制(示例文件：1.09G,基于字节流方式复制)
     * <ul>
     * <ui>FileInputStream+FileOutputStream 所用时间：8.526(秒)</ui>
     * <ui>BufferedInputStream+BufferedOutputStream 所用时间：2.923(秒)</ui>
     * <ui>FileChannel 所用时间：1.923(秒)</ui>
     * <ui>FileChannel+buffer 所用时间：1.795(秒)</ui>
     * <ui>Apache FileUtils.copyFile 所用时间：3.899(秒)</ui>
     * <ui>Java NIO Files.copy 所用时间：3.818(秒)</ui>
     * </ul>
     *
     * @throws IOException throw it
     */
    public static void testFileCopy() throws IOException {
        File source = new File("/Users/mac/Downloads/tmp/CentOS-7.iso");
        File fileInOrOutputStreamDest = new File("/Users/mac/Downloads/tmp/CentOS-7-M1.iso");
        File bufferedInOrOutputStreamDest = new File("/Users/mac/Downloads/tmp/CentOS-7-M4.iso");
        File fileInOrOutputStreamWithFileChannelDest = new File("/Users/mac/Downloads/tmp/CentOS-7-M2.iso");
        File fileInOrOutputStreamWithFileChannelAndMappedByteBufferDest = new File("/Users/mac/Downloads/tmp/CentOS-7-M3.iso");
        File apacheFileUtilsDest = new File("/Users/mac/Downloads/tmp/CentOS-7-M4.iso");
        File nioFilesDest = new File("/Users/mac/Downloads/tmp/CentOS-7-M5.iso");

        Monitor.begin();
        FileHelper.fileCopyByte(source, fileInOrOutputStreamDest, FileCopyByteEnum.FILE_IN_OUT_STREAM);
        Monitor.end(FileCopyByteEnum.FILE_IN_OUT_STREAM.getDesc());

        Monitor.begin();
        FileHelper.fileCopyByte(source, bufferedInOrOutputStreamDest, FileCopyByteEnum.BUFFERED_IN_OUT_STREAM);
        Monitor.end(FileCopyByteEnum.BUFFERED_IN_OUT_STREAM.getDesc());

        Monitor.begin();
        FileHelper.fileCopyByte(source, fileInOrOutputStreamWithFileChannelDest, FileCopyByteEnum.FILE_CHANNEL);
        Monitor.end(FileCopyByteEnum.FILE_CHANNEL.getDesc());

        Monitor.begin();
        FileHelper.fileCopyByte(source, fileInOrOutputStreamWithFileChannelAndMappedByteBufferDest, FileCopyByteEnum.FILE_CHANNEL_BUFFERED);
        Monitor.end(FileCopyByteEnum.FILE_CHANNEL_BUFFERED.getDesc());

        Monitor.begin();
        FileUtils.copyFile(source, apacheFileUtilsDest);
        Monitor.end("Apache FileUtils.copyFile");

        Monitor.begin();
        java.nio.file.Files.copy(source.toPath(), nioFilesDest.toPath());
        Monitor.end("Java NIO Files.copy");
    }

    /**
     * 测试文件分割与合并
     */
    @SneakyThrows
    public static void testFileSplitAndMerge() {
        System.out.println(FileHelper.getAllFileFromDir("/Users/mac/Downloads/tmp"));
        System.out.println(FileHelper.sizeByteFormat("1048576"));

        File source = new File("/Users/mac/Downloads/tmp/file_io_test/CentOS-7.iso");
        File target = new File("/Users/mac/Downloads/tmp/file_io_test");
        File newMerge1 = new File("/Users/mac/Downloads/tmp/file_io_test/CentOS-7-merge1.iso");
        File newMerge2 = new File("/Users/mac/Downloads/tmp/file_io_test/CentOS-7-merge2.iso");

        Monitor.begin();
        FileHelper.splitFile(source, target);
        Monitor.end("split file ");

        Monitor.begin();
        FileHelper.mergeFile(target, newMerge1);
        Monitor.end("merge file clip");

        Monitor.begin();
        FileHelper.mergeFileBySeq(target, newMerge2);
        Monitor.end("merge file clip by seq");
    }

}
